<template>
    <div>
        <div style="width: 100%;" v-show="worklog==1">
            <el-page-header @back="goBack" content="安全检查记录列表"></el-page-header>
            <el-divider></el-divider>
            <el-form :inline="true" :model="securitycheckfrom">
                <el-form-item>
                    <el-select v-model="securitycheckfrom.id" placeholder="安检编号">
                        <el-option label="--请选择--" value></el-option>
                        <el-option
                                v-for="item in securitycheck"
                                :key="item.id"
                                :label="item.id"
                                :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item style="margin-left: 20px">
                    <el-input placeholder="检查情况" v-model="securitycheckfrom.sceneCheckSituation"></el-input>
                </el-form-item>
                <el-form-item style="margin-left: 20px">
                    <el-input placeholder="处理要求" v-model="securitycheckfrom.handleMeasure"></el-input>
                </el-form-item>
                <el-form-item style="margin-left: 20px">
                    <el-select v-model="securitycheckfrom.projectName" placeholder="项目名称">
                        <el-option label="--请选择--" value></el-option>
                        <el-option
                                v-for="item in projectname"
                                :key="item.id"
                                :label="item.projectName"
                                :value="item.projectName"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item style="margin-left: 20px">
                    <el-input placeholder="检查人" v-model="securitycheckfrom.checker"></el-input>
                </el-form-item>
                <el-form-item style="margin-left: 20px">
                    <el-date-picker
                            type="date"
                            placeholder="起止日期"
                            value-format="yyyy-MM-dd"
                            v-model="securitycheckfrom.updateDate"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item style="margin-left: 20px">
                    <el-select placeholder="审批状态" v-model="securitycheckfrom.approveStatus">
                        <el-option label="审批状态" value></el-option>
                        <el-option label="审批中" value="1"></el-option>
                        <el-option label="审批不通过" value="2"></el-option>
                        <el-option label="审批通过" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item style="margin-left: 20px">
                    <el-button type="primary" @click="SecurityCheckList">查询</el-button>
                    <el-button type="primary" @click="add">新增</el-button>
                    <el-button type="primary" @click="exportExcel">导出</el-button>
                    <el-button type="primary" @click="dy">打印</el-button>
                </el-form-item>
            </el-form>

            <el-table
                    id="out-table"
                    :data="securitycheck.slice((currentPage-1)*pagesize,currentPage*pagesize)"
            >
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="安检编号" prop="id"></el-table-column>
                <el-table-column label="项目名称" prop="projectName"></el-table-column>
                <el-table-column label="现场检查情况" prop="sceneCheckSituation"></el-table-column>
                <el-table-column label="处理要求及措施" prop="handleMeasure"></el-table-column>
                <el-table-column label="日期" prop="updateDate"></el-table-column>
                <el-table-column label="检查人" prop="checker"></el-table-column>
                <el-table-column label="流程状态" prop="approveStatus">
                    <template slot-scope="scope">
                        <span style="color: orange;font-weight: 700" v-if="scope.row.approveStatus=='1'">审批中</span>
                        <span style="color: red;font-weight: 700" v-if="scope.row.approveStatus=='2'">审批不通过</span>
                        <span style="color: green;font-weight: 700" v-if="scope.row.approveStatus=='3'">审批通过</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-dropdown>
                            <el-button type="primary">
                                操作
                                <i class="el-icon-arrow-down el-icon--right"></i>
                            </el-button>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item @click.native="getSecurityCheckId(scope)">查看详情</el-dropdown-item>
                                <el-dropdown-item @click.native="updateSecurityCheckId(scope)">修改</el-dropdown-item>
                                <el-dropdown-item @click.native="del(scope)">删除</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[2, 4, 8, 10]"
                    :page-size="pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="securitycheck.length"
            >//这是显示总共有多少数据
            </el-pagination>
        </div>
        <!--查看 -->
        <!-- 更新对话框 -->
        <!-- 3.使用子组件 -->
        <SecurityCheckXq
                v-bind:getsecuritycheckid="getsecuritycheckid"
                v-if="worklog==2"
                :worklog="worklog"
                @colse="cols"
                :securitychecks="securitychecks"
        ></SecurityCheckXq>
        <!--修改 -->
        <!-- 更新对话框 -->
        <!-- 3.使用子组件 -->
        <SecurityCheckUpdate
                v-bind:getsecuritycheckid="getsecuritycheckid"
                v-if="worklog==3"
                :worklog="worklog"
                @colse="cols"
                @update="update"
                :projectname="projectname"
                :securitychecks="securitychecks"
        ></SecurityCheckUpdate>
    </div>
</template>
<script>
    import axios from "axios";
    import SecurityCheckXq from "../../zxm/SecurityCheckRecordUpdate/SecurityCheckXq.vue";
    import SecurityCheckUpdate from "../../zxm/SecurityCheckRecordUpdate/SecurityCheckUpdate.vue";
    import {AttachemenInfoService} from "../../../model/gs/AttachemenInfoService";

    const attachemenInfoService = AttachemenInfoService.getInstance();
    // 引入导出Excel表格依赖
    import FileSaver from "file-saver";
    import XLSX from "xlsx";

    export default {
        components: {SecurityCheckXq, SecurityCheckUpdate}, //注册子组件
        data() {
            return {
                securitycheckfrom: {},
                securitycheck: [],
                projectname: [],
                getsecuritycheckid: {},
                worklog: 1,
                currentPage: 1, //初始页
                pagesize: 4, //    每页的数据
                securitychecks: []
            };
        },
        methods: {
            //定义导出Excel表格事件
            exportExcel() {
                /* 从表生成工作簿对象 */
                let wb = XLSX.utils.table_to_book(document.querySelector("#out-table"));
                /* 获取二进制字符串作为输出 */
                var wbout = XLSX.write(wb, {
                    bookType: "xlsx",
                    bookSST: true,
                    type: "array"
                });
                try {
                    FileSaver.saveAs(
                        //Blob 对象表示一个不可变、原始数据的类文件对象。
                        //Blob 表示的不一定是JavaScript原生格式的数据。
                        //File 接口基于Blob，继承了 blob 的功能并将其扩展使其支持用户系统上的文件。
                        //返回一个新创建的 Blob 对象，其内容由参数中给定的数组串联组成。
                        new Blob([wbout], {type: "application/octet-stream"}),
                        //设置导出文件名称
                        "安全检查记录.xlsx"
                    );
                } catch (e) {
                    if (typeof console !== "undefined") console.log(e, wbout);
                }
                return wbout;
            },
            //打印
            dy() {
                window.print();
            },
            // 初始页currentPage、初始每页数据数pagesize和数据data
            handleSizeChange: function (size) {
                this.pagesize = size;
            },
            handleCurrentChange: function (currentPage) {
                this.currentPage = currentPage;
            },
            cols(a) {
                this.worklog = a;
            },
            goBack() {
                history.back();
            },
            //查询安全记录列表
            SecurityCheckList() {
                axios
                    .get(`http://localhost/SecurityCheck`, {
                        params: this.securitycheckfrom
                    })
                    .then(response => {
                        this.securitycheck = response.data.data;
                    });
            },
            //查询项目名称并渲染到下拉框
            ProjectNameList() {
                axios.get(`http://localhost/SecurityLog/ProjectName`).then(response => {
                    this.projectname = response.data.data;
                });
            },
            //删除安全检查记录信息
            del(securitycheck) {
                axios
                    .delete(`http://localhost/SecurityCheck/${securitycheck.row.id}`)
                    .then(() => {
                        this.SecurityCheckList();
                        this.$message("删除成功");
                    });
            },
            //查询详情
            getSecurityCheckId(securitycheck) {
                this.getsecuritycheckid = securitycheck.row;
                attachemenInfoService
                    .getById(this.getsecuritycheckid.attachIds)
                    .then(re => {
                        this.securitychecks = re.data.data;
                        this.worklog = 2;
                    });
            },
            //查询一条数据
            updateSecurityCheckId(securitycheck) {
                this.getsecuritycheckid = securitycheck.row;
                attachemenInfoService
                    .getById(this.getsecuritycheckid.attachIds)
                    .then(re => {
                        this.securitychecks = re.data.data;
                        this.worklog = 3;
                    });
            },
            //新增
            add() {
                this.$router.push({path: "/SecurityCheckRecord"});
            },
            //修改
            update(getsecuritycheckid) {
                axios
                    .put(`http://localhost/SecurityCheck`, getsecuritycheckid)
                    .then(() => {
                        this.SecurityCheckList();
                        this.worklog = 1;
                        this.$message("修改成功");
                    });
            }
        },
        created() {
            this.SecurityCheckList();
            this.ProjectNameList();
        }
    }
</script>

<style scoped>
</style>
